﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vowei.Data.Converters;
using System.IO;
using Vowei.Core;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;

namespace Vowei.Converters.Excel
{
    public abstract class DataConnectionConverter : FormatConverter
    {
        protected string ConnectionName { get; set; }

        protected DataConnectionConverter(ITranslator translator)
            : base(translator)
        {
        }

        public override bool CanConvert(object stream)
        {
            throw new NotImplementedException();
        }

        public override object ConvertFrom(object from)
        {
            throw new NotImplementedException();
        }

        public override void ConvertTo(object from, object to)
        {
            ConvertTo((string)from, (Stream)to);
        }

        private string _statisticsUrl = null;

        public string StatisticsUrl
        {
            get { return _statisticsUrl; }
            set { _statisticsUrl = value; }
        }

        public void ConvertTo(string pageUrl, Stream excelOut)
        {
            if (string.IsNullOrEmpty(pageUrl))
                throw new ArgumentNullException("pageUrl");
            if (excelOut == null)
                throw new ArgumentNullException("excelOut");

            _statisticsUrl = pageUrl;
            using (SpreadsheetDocument package = SpreadsheetDocument.Create(excelOut, SpreadsheetDocumentType.Workbook))
            {
                CreateParts(package);
            }
        }

        protected abstract void CreateParts(SpreadsheetDocument document);

        protected void SetPackageProperties(OpenXmlPackage document)
        {
        }
    }
}
